package com.example.xwuser.service.impl;


import com.example.xw.common.entity.User;
import com.example.xw.common.service.UserService;
import com.example.xw.common.vo.Result;
import com.example.xwuser.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("myUserServiceImpl")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Result<User> reg(String nickname, String loginname, String loginpwd, String photo) {
        User user = new User();
        user.setNickname(nickname);
        user.setLoginname(loginname);
        user.setLoginpwd(loginpwd);
        user.setPhoto(photo);
        user.setScore(50);
        user.setState(1);
        userMapper.insert(user);
        return new Result<>(2000,user,"");

    }

    @Override
    public Result<User> login(String loginname, String loginpwd) {
        User user = userMapper.getUserByLoginname(loginname);
        if (user == null) {
            return new Result<User>(4001, null, "用户不存在");
        }
        if (!user.getLoginpwd().equals(loginpwd)) {
            return new Result<User>(4001,null,"密码错误");
        }
        return new Result<>(2000,user,"");
    }

    @Override
    @Transactional
    public Result<User> addScore(String loginname, int score) {
        User user = userMapper.getUserByLoginname(loginname);
        if (user == null) {
            return new Result<User>(4001, null, "用户不存在");
        }
        user.setScore(user.getScore() + score);
        userMapper.updateById(user);
        return new Result<>(2000,user,"");

    }


    @Override
    @Transactional
    public Result<User> updatestate(String loginname, int state) {
        User user = userMapper.getUserByLoginname(loginname);
        if (user == null) {
            return new Result<User>(4001, null, "用户不存在");
        }
        user.setState(state);
        userMapper.updateById(user);
        return new Result<>(2000,user,"");

    }

    @Override
    public Result<User> getUser(String loginname) {
        User user = userMapper.getUserByLoginname(loginname);
        if (user == null) {
            return new Result<User>(4001, null, "用户不存在");
        }
        return new Result<>(2000,user,"");

    }

    @Override
    public Result<User> getUserByLoginname(String loginname) {
        User user = userMapper.getUserByLoginname(loginname);
        if (user == null) {
            return new Result<User>(4001, null, "用户不存在");
        }
        return new Result<>(2000,user,"");

    }




}
